package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	num:=1234567891
	fmt.Println(numberToWords(num))
//	"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
}

var (
	NumMap = map[int]string{
		1:"One",
		2:"Two",
		3:"Three",
		4:"Four",
		5:"Five",
		6:"Six",
		7:"Seven",
		8:"Eight",
		9:"Nine",
		10:"Ten",
		11:"Eleven",
		12:"Twelve",
		13:"Thirteen",
		14:"Fourteen",
		15:"Fifteen",
		16:"Sixteen",
		17:"Seventeen",
		18:"Eighteen",
		19:"Nineteen",
		20:"Twenty",
		30:"Thirty",
		40:"Forty",
		50:"Fifty",
		60:"Sixty",
		70:"Seventy",
		80:"Eighty",
		90:"Ninety",
		100:"Hundred",
		1000:"Thousand",
		10_000_000:"Million",
		1_000_000_000:"Billion",
	}
)

var (
	singles   = []string{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
	teens     = []string{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}
	tens      = []string{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
	thousands = []string{"", "Thousand", "Million", "Billion"}
)
// 单位的分割未考虑清楚
func numberToWords(num int) string {
	if num == 0 {
		return "Zero"
	}
	sb := &strings.Builder{}
	//计算百,十,千
	toEnglish := func(num int) {
		if num >= 100 {
			sb.WriteString(singles[num/100])
			sb.WriteString(" Hundred ")
			num %= 100
		}
		if num >= 20 {
			sb.WriteString(tens[num/10])
			sb.WriteByte(' ')
			num %= 10
		}
		if 0 < num && num < 10 {
			sb.WriteString(singles[num])
			sb.WriteByte(' ')
		} else if num >= 10 {
			//小于20的部分
			sb.WriteString(teens[num-10])
			sb.WriteByte(' ')
		}
	}
	for i, unit := 3, int(1e9); i >= 0; i-- {
		if curNum := num / unit; curNum > 0 {
			//最小单位前的数字
			num -= curNum * unit
			toEnglish(curNum)
			sb.WriteString(thousands[i])
			sb.WriteByte(' ')
		}
		unit /= 1000
	}
	return strings.TrimSpace(sb.String())
}

/**
 * @Description: 计算最小单位
 */
func countMinUnit(num int,) []string {
	ans:=""
	i:=float64(0)
	for num>0{
		modNum:=num%10
		ans = NumMap[modNum*int(math.Pow(10,i))]+" "+ans
		num/=10
		i++
	}
	//先除亿

	// 亿 计算 百万

	// 百万 计算 千

	// 千 计算 百

	// 百 计算 个位数

	// 个位数取 map

	// 两位数 取前一位
	return nil
}


